<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    include ("../include/menu.php");
    include('../include/dbFunctions.php');
    include('../include/formFunctions.php');
    $mes="";
    if (isset($_SESSION['mes'])) {
        $mes = $_SESSION['mes'];
        $_SESSION['mes']="";
    }
//GET school ID from URL you just clicked
    $school_id = $_GET['id']; //No need to use mysql_real_escape_string since PDO is already catered for it using PREPARED statement
    $_SESSION['id'] = $school_id; //Make $id become GLOBAL for passing to next page for UPDATE or DELETE
    $_SESSION['school_id'] = $school_id;
    /* Query school details from database */
    $query1 = "SELECT s.school_name, s.school_postal, s.school_address, e.education_name, s.school_description, s.school_status,l.location_name
           FROM school s, education e, location l
           WHERE s.education_id = e.education_id
           AND l.location_id=s.location_id
           AND s.school_id = $school_id";
    $result1 = executeSelectQuery($query1);

    $school_name = $result1[0]['school_name'];
    $address = $result1[0]['school_address'];
    $education = $result1[0]['education_name'];
    $description = $result1[0]['school_description'];
    $status = $result1[0]['school_status'];
    $location = $result1[0]['location_name'];
    $postal = $result1[0]['school_postal'];

    /* In the same time, query the school contacts from contact table */
    $query2 = " SELECT *
            FROM contact_school
            WHERE school_id = $school_id";
    $result2 = executeSelectQuery($query2);

    /* Query events for a school */
    $query3 = " SELECT *
    FROM event e, school s
    WHERE e.school_id = s.school_id
    AND s.school_id = $school_id";
    $result3 = executeSelectQuery($query3);


    $query = "SELECT * FROM event e,school s WHERE
e.school_id = s.school_id
AND s.school_id=$school_id
AND e.event_status = 'confirmed' ORDER BY e.created_date DESC";
    $event_array = executeSelectQuery($query);


    ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>School Details</title>

        <!--Embed CSS Scripts!-->
        <style type="text/css" title="currentStyle">
            @import "../media/css/dataTable.css";
            @import "../media/css/jquery-ui-1.8.13.custom.css";
            @import "../media/css/screen.css";
        </style>

        <!--Embed Javascripts-->
        <script type="text/javascript" src="../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="../media/js/jquery/jquery-ui-1.8.13.custom.js"></script>
        <script type="text/javascript" src="../media/js/jquery/jquery.dataTables.min.js"></script>
        <script type="text/javascript" src="../media/js/jquery/ajaxReloadDataTable.js"></script>
        <script type="text/javascript" src="../media/js/jquery/jquery.dataTables.editable.js"></script>
        <script type="text/javascript" src="../media/js/jquery/jquery.validate.js"></script>
        <script type="text/javascript" src="../media/js/jquery/jquery.jeditable.js"></script>


        <!--Onsite scripts-->
        <script type="text/javascript" charset="utf-8">
            function deleteRecord(){
                var deleteURL = 'schoolDelete.php';
                var redirectURL = 'schoolManage.php';
                var confirmation = confirm("Are you sure you want to delete this record?");
                if(confirmation){
                    $.ajax({
                        type: 'GET',
                        url: deleteURL,
                        success: function(data){
                            if(data==1){
                                alert("Delete Successfully");
                                window.location.replace(redirectURL);
                            }else{
                                alert("An error was occured! Could not delete school!");
                            }
                        }
                    });
                }//End if(confirmation)
            };

            $(document) .ready(function(){
                //Script to manage school events table
                $("#school_events_table").dataTable({"sPaginationType": "full_numbers"});
                $("#school_contac_table").dataTable({"sPaginationType": "full_numbers"});


            });


        </script>
    </head>
    <body>
        <!-- DON'T TOUCH! Start: page-top-outer -->
            <?php
            top_outer();
            ?>
        <!-- End: page-top-outer -->

        <div class="clear">&nbsp;</div>

        <!--  start nav-outer-repeat................................................................................................. START -->
        <div class="nav-outer-repeat">
            <!--  start nav-outer -->
            <div class="nav-outer">

                <!-- start nav-right -->
                    <?php
                    nav_right();
                    ?>
                <!-- end nav-right -->


                <!--  start nav -->
                <div class="nav">
                    <div class="table">

                            <?php
                            navigation_school();
                            ?>

                        <div class="clear"></div>
                    </div>
                    <div class="clear"></div>
                </div>
                <!--  start nav -->

            </div>
            <div class="clear"></div>
            <!--  start nav-outer -->
        </div>
        <!--  start nav-outer-repeat................................................... END -->

        <div class="clear"></div>

        <!-- start content-outer ........................................................................................................................START -->
        <div id="content-outer">
            <!-- start content -->
            <div id="content">

                <!--  start page-heading --> <!--lyedit-->
                <div id="page-heading">
                    <h1>View School</h1>
                </div>
                <!-- end page-heading -->

                <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                    <tr>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                        <th class="topleft"></th>
                        <td id="tbl-border-top">&nbsp;</td>
                        <th class="topright"></th>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                    </tr>
                    <tr>
                        <td id="tbl-border-left"></td>
                        <td>
                            <!--  start content-table-inner ...................................................................... START -->
                            <div id="content-table-inner">
                                <h2></h2>
                                <!--  start table-content  -->
                                <div id="table-content">
                                        <?php echo "<b><font color =red>$mes</font></b><br/>" ?>
                                    <div id="details_content">
                                        <!--  Can put table start from here within this div-->
                                        <h3>School Information<a href="schoolEdit.php?id=<?php echo $school_id ?>"><img src='../media/images/forms/edit-icon.png' width=15></img></a></h3>
                                        <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
                                            <tr>
                                                <th valign="top">School</th>
                                                <td class="school_name" id="school_name"><?php echo $school_name; ?></td>
                                            </tr>
                                            <tr>
                                                <th valign="top">Address</th>
                                                <td class="school_address" id="school_address"><?php echo $address; ?></td>
                                            </tr>
                                            <tr>
                                                <th valign="top">Postal Code</th>
                                                <td class="school_postal" id="school_postal"><?php echo $postal; ?></td>
                                            </tr>
                                            <tr>
                                                <th valign="top">Location</th>
                                                <td class="school_address" id="school_address"><?php echo $location; ?></td>
                                            </tr>
                                            <tr>
                                                <th valign="top">Education Level</th>
                                                <td class="education_id" id="education_id"><?php echo $education; ?></td>
                                            </tr>
                                            <tr>
                                                <th valign="top">School Description</th>
                                                <td class="school_description" id="school_description"><?php echo $description; ?></td>
                                            </tr>
                                            <tr>
                                                <th valign="top">Status</th>
                                                <td class="school_status" id="school_status"><?php echo $status; ?></td>
                                            </tr>

                                        </table>

                                        <!--End of #school_details!-->
                                        <br/>

                                        <h3>School Course</h3>

                                        <table id="school_events_table" class="display" cellpadding="0" cellspacing="0" border="0">
                                            <thead>
                                                <tr>
                                                    <th>No.</th>
                                                    <th>Course</th>
                                                    <th>Status</th>
                                                    <th>No. Class</th>
                                                    <th>No. Students</th>

                                                </tr>
                                            </thead>
                                            <tbody>
                                                    <?php
                                                    for ($i=0;$i<count($result3);$i++) {
                                                        $id = $result3[$i]['event_id'];
                                                        $no = $i+1;
                                                        $event = $result3[$i]['event_name'];
                                                        $status = eventStatus($result3[$i]['event_status']);

                                                        //no student & no class
                                                        $query3= "SELECT class_no_student FROM class WHERE event_id = $id";
                                                        $student_array = executeSelectQuery($query3);
                                                        $no_class = count($student_array);
                                                        $no_student = 0;
                                                        for ($i=0;$i<$no_class;$i++) {
                                                            $no_student +=$student_array[$i]['class_no_student'];
                                                        }


                                                        //first last
                                                        $query51 = "SELECT * FROM session WHERE event_id = ".$id;
                                                        $session_array = executeSelectQuery($query51);
                                                        $first = $last = $session_array[0]['session_date'];
                                                        for ($i=0;$i<count($session_array);$i++) {
                                                            if ($session_array[$i]['session_date']<$first) {
                                                                $first = $session_array[$i]['session_date'];
                                                            }
                                                            if ($session_array[$i]['session_date']>$last) {
                                                                $last = $session_array[$i]['session_date'];
                                                            }
                                                        }
                                                        //

                                                        echo "<tr>";
                                                        echo "<td>$no</td>";
                                                        echo "<td><a href='../event/eventView.php?id=".$id."'>$event</a></td>";
                                                        echo "<td>$status</td>";
                                                        echo "<td>$no_class</td>";
                                                        echo "<td>$no_student</td>";
                                                        ?>
                                                        <?php
                                                    }
                                                    ?>
                                            </tbody>
                                        </table><br/>
                                        <br/><br/><br/>
                                        <h3>School Contact</h3>


                                        <table id="school_contac_table" class="display" cellpadding="0" cellspacing="0" border="0">
                                            <thead>
                                                <tr>
                                                    <th>Contact Name</th>
                                                    <th>Contact Salutation</th>
                                                    <th>Contact Designation</th>
                                                    <th>Office Number</th>
                                                    <th>Mobile Number</th>
                                                    <th>Contact Email</th>
                                                    <th>Status</th>
                                                    <th></th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                    <?php
                                                    for ($i = 0; $i < count($result2); $i++) {
                                                        $contact_id = $result2[$i]['contact_school_id'];
                                                        $contact_name = $result2[$i]['contact_name'];
                                                        $contact_email = $result2[$i]['contact_email'];
                                                        $contact_salutation = $result2[$i]['contact_salutation'];
                                                        $contact_designation = $result2[$i]['contact_designation'];
                                                        $office_number = $result2[$i]['office_number'];
                                                        $mobile_number = $result2[$i]['mobile_number'];
                                                        $contact_status = $result2[$i]['contact_status'];

                                                        $sendemail = "<a href='mailto:$contact_email'>$contact_email</>";

                                                        echo "<tr id='" . $contact_id . "'>";

                                                        echo "<td>" . $contact_name . "<a href='contact/editContact.php?contact_id=$contact_id'><img src='../media/images/forms/edit-icon.png' width=15></img></td>";
                                                        echo "<td>" . $contact_salutation . "</td>";
                                                        echo "<td>" . $contact_designation . "</td>";
                                                        echo "<td>" . $office_number . "</td>";
                                                        echo "<td>" . $mobile_number . "</td>";
                                                        echo "<td>" . $sendemail . "</td>";
                                                        echo "<td>" . getVolunteerMainStatus($contact_status) . "</td>";
                                                        ?>
                                                <td><a href='contact/deleteContact.php?id= <?php echo $contact_id?>' onclick="return  confirm('You sure you want to delete this contact?')">Delete</a></td>
                                                        <?php
                                                        echo "</tr>";
                                                    }
                                                    ?>
                                            </tbody>
                                        </table>
                                        <!--End of #school_contact_details!-->

                                        <table>
                                            <tr>
                                                <th>
                                                    <td>
                                                        <input type="button" class="form-back" value="&nbsp" onclick="window.location.replace('schoolManage.php')"/>
                                                    </td>

                                                    <td>
                                                        <form method="GET" id="submitform" action="schoolDelete.php" >
                                                            <input type="hidden" name="id" value='<?php echo $school_id;?>'></input>
                                                            <input type="submit" class="form-delete" onClick="if (confirm('Are you sure you want to delete?')) commentDelete(1); return false" value="&nbsp"/>
                                                        </form>
                                                    </td>
                                                    <td>
                                                        <input type="button" value="&nbsp" onclick="window.location.replace('contact/addContact.php')" class="add-contact1"/>
                                                    </td>
                                                </th>
                                            </tr>
                                        </table>

                                        <br/><br/>

                                    </div>
                                    <!--  end table-content here -->
                                </div>
                                <div class="clear"></div>
                            </div>
                            <!--  end content-table-inner ............................................END  -->
                        </td>
                        <td id="tbl-border-right"></td>
                    </tr>
                    <tr>
                        <th class="sized bottomleft"></th>
                        <td id="tbl-border-bottom">&nbsp;</td>
                        <th class="sized bottomright"></th>
                    </tr>
                </table>

                <div class="clear">&nbsp;</div>
            </div><!--  end content -->

            <div class="clear">&nbsp;</div>
        </div>
        <!--  end content-outer........................................................END -->
            <?php
            footer();
            ?>

    </body>
</html>

    <?php
}else {
    header("Location: ../include/login.html");
}
?>